#!/usr/bin/env python
import dbus, dbus.service
import gui.add, gui.browser, gui.prefs
import dieu.database, dieu.util
from xml.sax.saxutils import unescape

class DieuListen(dbus.service.Object):
    def __init__(self, bus_name, trayicon):
        dbus.service.Object.__init__(self, bus_name, '/org/gnome/Dieu')
        self.trayicon = trayicon
    
    @dbus.service.method('org.gnome.Dieu')
    def AddBookmark(self, Title, URL, Extended, Tags):
        Title = unescape(Title, {"&apos;": "'", "&quot;": '"'})
        URL = unescape(URL, {"&apos;": "'", "&quot;": '"'})
        Extended = unescape(Extended, {"&apos;": "'", "&quot;": '"'})
        gui.add.DieuAddPost(Title, URL, Extended, Tags)
    
    @dbus.service.method('org.gnome.Dieu')
    def LaunchBrowser(self, user):
        gui.browser.DieuBrowser(user)
    
    @dbus.service.method('org.gnome.Dieu')
    def ShowTray(self, quoi):
        if quoi == True:
            self.trayicon.trayicon.set_visible(True)
        elif quoi == False:
            self.trayicon.trayicon.set_visible(False)
    
    @dbus.service.method('org.gnome.Dieu')
    def GetPosts(self, user):
        db = dieu.database.DieuDatabase(user.split(':')[0], user.split(':')[1])
        return db.get_all_posts('Default')
    
    @dbus.service.method('org.gnome.Dieu')
    def GetTags(self, user):
        db = dieu.database.DieuDatabase(user.split(':')[0], user.split(':')[1])
        return db.get_all_tags('Alphabetical')
    
    @dbus.service.method('org.gnome.Dieu')
    def Sync(self):
        return dieu.util.sync()
    
    @dbus.service.method('org.gnome.Dieu')
    def NewAccount(self):
        gui.prefs.DieuNewAccount()
    
    @dbus.service.method('org.gnome.Dieu')
    def Search(self, user, term):
        db = dieu.database.DieuDatabase(user.split(':')[0], user.split(':')[1])
        return db.search(term)
        
